package com.zhx.socketio.socketio.socketio;

import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketConfig;
import com.corundumstudio.socketio.SocketIOServer;
import com.zhx.socketio.socketio.config.WebBaseConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service("chatServer")
public class ChatServer implements InitializingBean {

    @Resource
    private EventListenner eventListenner;


    @Autowired(required = false)
    private WebBaseConfig webBaseConfig;

    @Override
    public void afterPropertiesSet() throws Exception {
        if(null != webBaseConfig){
            log.info("---------------监测到配置 socketio 开始连接 -------------");

            Configuration config = new Configuration();
            config.setPort(webBaseConfig.getPort());

            SocketConfig socketConfig = new SocketConfig();
            socketConfig.setReuseAddress(true);
            socketConfig.setTcpNoDelay(true);
            socketConfig.setSoLinger(0);
            config.setSocketConfig(socketConfig);
            config.setHostname(webBaseConfig.getHost());

            SocketIOServer server = new SocketIOServer(config);
            server.addListeners(eventListenner);
            server.start();
            System.out.println("启动正常");
        }
    }
}
